package com.gameplatform.config;

import com.gameplatform.entity.User;
import com.gameplatform.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public void run(String... args) {
        // 只有当用户表为空时才初始化数据
        if (userRepository.count() == 0) {
            // 创建测试用户1
            User user1 = new User();
            user1.setUsername("test1");
            user1.setPassword(passwordEncoder.encode("123456"));
            user1.setNickname("测试用户1");
            user1.setScore(1000);
            userRepository.save(user1);

            // 创建测试用户2
            User user2 = new User();
            user2.setUsername("test2");
            user2.setPassword(passwordEncoder.encode("123456"));
            user2.setNickname("测试用户2");
            user2.setScore(2000);
            userRepository.save(user2);

            // 创建管理员用户
            User admin = new User();
            admin.setUsername("admin");
            admin.setPassword(passwordEncoder.encode("admin123"));
            admin.setNickname("管理员");
            admin.setScore(9999);
            userRepository.save(admin);

            System.out.println("测试数据初始化完成！");
            System.out.println("可用的测试账号：");
            System.out.println("普通用户1 - username: test1, password: 123456");
            System.out.println("普通用户2 - username: test2, password: 123456");
            System.out.println("管理员 - username: admin, password: admin123");
        }
    }
} 